package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.mapper.DtsRegionMapper;
import com.qf.pojo.DtsBrand;
import com.qf.service.DtsBrandService;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/23 22:52
 */
@Service
public class DtsBrandServiceImpl implements DtsBrandService {
    @Autowired
    private DtsBrandMapper dtsBrandMapper;
    @Override
    public Page<DtsBrand> selectPage(Integer page, Integer limit, String sort, String order, String name, Integer id) {
        Page<DtsBrand> dtsBrandPage = new Page<DtsBrand>(page, limit);

        QueryWrapper<DtsBrand> dtsBrandQueryWrapper = new QueryWrapper<>();
        dtsBrandQueryWrapper.orderBy(!StringUtils.isEmpty(sort),"asc".equalsIgnoreCase(order),sort).like(!StringUtils.isEmpty(name),"name",name).eq(id!=null,"id",id).eq("deleted", false);
        return dtsBrandMapper.selectPage(dtsBrandPage,dtsBrandQueryWrapper);
    }

    @Override
    public void insert(DtsBrand dtsBrand) {
        dtsBrandMapper.insert(dtsBrand);
    }

    @Override
    public void update(DtsBrand dtsBrand) {
        dtsBrandMapper.updateById(dtsBrand);
    }

    @Override
    public void deleteBrand(DtsBrand dtsBrand) {
        dtsBrandMapper.updateById(dtsBrand);
    }

    @Override
    public Set<CatVo> select() {
        List<DtsBrand> dtsBrands = dtsBrandMapper.selectList(null);
       return dtsBrands.stream().map(dtsBrand -> {
            CatVo catVo = new CatVo();
            catVo.setValue(dtsBrand.getId());
            catVo.setLabel(dtsBrand.getName());
            return catVo;
        }).collect(Collectors.toSet());
        //return null;
    }
}
